package com.traffic.util;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.api.R;
import com.traffic.entity.RtUserData;
import com.traffic.entity.User;
import com.traffic.entity.UserInfo;
import com.traffic.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.jws.soap.SOAPBinding;
import java.util.ArrayList;
import java.util.List;



/**
 * Created by IntelliJ IDEA.
 * User: KingRainGrey
 * Date: 2020/10/30
 */
@Component
public class DataUtil {
    @Autowired
    UserInfoService userInfoService;

    /**
     * 传入user列表 装载userinfo
     * @param list
     * @return
     */
    public List<RtUserData> dataChange(List<User> list) {
        List<RtUserData> data = new ArrayList<>();
        if (ObjectUtil.isNotNull(list) && list.size() > 0) {
            list.forEach(user -> {
                UserInfo userInfo = userInfoService.getUserInfoByUserId(user.getId());
                if (ObjectUtil.isNotNull(userInfo)) {
                    RtUserData rtUserData = give(user, userInfo);
                    data.add(rtUserData);
                }
            });
            return data;
        }
        return null;
    }

    /**
     * 传入 user 包装
     * @param user
     * @return
     */
    public RtUserData dataChange(User user) {
        if (ObjectUtil.isNotNull(user)) {
            UserInfo userInfo = userInfoService.getUserInfoByUserId(user.getId());
            if (ObjectUtil.isNotNull(userInfo)) {
                return give(user, userInfo);
            }
        }
        return null;
    }

    public RtUserData give(User user, UserInfo userInfo) {
        RtUserData data = RtUserData.builder()
                .account(user.getAccount())
                .birthday(userInfo.getBirthday())
                .city(userInfo.getCity())
                .createTime(user.getCreateTime())
                .driveAge(userInfo.getDriveAge())
                .headImg(userInfo.getHeadImg())
                .id(user.getId())
                .lastLoginTime(user.getLastLoginTime())
                .lastUpdateTime(userInfo.getLastUpdateTime())
                .nickName(userInfo.getNickName())
                .phone(userInfo.getPhone())
                .profile(userInfo.getProfile())
                .rank(userInfo.getRank())
                .role(user.getRole())
                .sex(userInfo.getSex())
                .status(user.getStatus())
                .build();
        return data;
    }
}
